<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button @click="beforeAddVehicle"
               type="primary" style="float:right;margin-top:13px;">新建车辆</el-button>
  </div>
  <!-- 新增车辆弹窗 :before-close="handleClose" 在弹窗关闭之前调用此定义函数
这样避免点击弹窗外部也能将弹窗关闭的问题-->
  <el-dialog :title="dialogTitle" v-model="dialogVisible"
             style="width:1000px;padding:40px; "
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容" v-model="saveVehicleFrom.brand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input placeholder="请输入内容" v-model="saveVehicleFrom.license"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input placeholder="请输入内容" v-model="saveVehicleFrom.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input placeholder="请输入内容" v-model="saveVehicleFrom.code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select placeholder="请选择" v-model="saveVehicleFrom.displacement">
              <el-option label="1.6" value="1"></el-option>
              <el-option label="2.5" value="2"></el-option>
              <el-option label="4" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select placeholder="请选择" v-model="saveVehicleFrom.type">
              <el-option v-for="item in vehicleTypeArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="轿车" value="10"></el-option>-->
<!--              <el-option label="货车" value="20"></el-option>-->
<!--              <el-option label="客车" value="30"></el-option>-->
<!--              <el-option label="挂车" value="40"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select placeholder="请选择" v-model="saveVehicleFrom.color">
              <el-option label="黑" value="10"></el-option>
              <el-option label="白" value="20"></el-option>
              <el-option label="蓝" value="30"></el-option>
              <el-option label="灰" value="40"></el-option>
              <el-option label="银" value="50"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input placeholder="请输入内容" v-model="saveVehicleFrom.kilometers"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间" >
            <el-date-picker v-model="saveVehicleFrom.buyTime" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间" >
            <el-date-picker v-model="saveVehicleFrom.regTime" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="saveVehicleFrom.price"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select placeholder="请选择" v-model="saveVehicleFrom.batteryType">
              <el-option v-for="item in batteryTypeArr" :label="item.label" :value="item.value"></el-option>
<!--              <el-option label="铅酸蓄电池" value="10"></el-option>-->
<!--              <el-option label="镍氢电池" value="20"></el-option>-->
<!--              <el-option label="钠硫电池" value="30"></el-option>-->
<!--              <el-option label="二次锂电池" value="40"></el-option>-->
<!--              <el-option label="空气电池" value="50"></el-option>-->
<!--              <el-option label="三元锂电池" value="60"></el-option>-->
<!--              <el-option label="碱性燃料电池" value="70"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button><!--关闭弹窗的功能-->
      <el-button type="primary" @click="saveVehicle">保存</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;" >
    <el-form :inline="true">
      <el-form-item label="车辆品牌" >
        <el-input placeholder="请输入内容"
                  v-model="searchVehicleForm.brand"
                  @keydown.enter="loadVehicle"
                  style="width:220px;"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:290px;" >
        <el-input placeholder="请输入内容"
                  style="width:220px;"
                  v-model="searchVehicleForm.license"
                  @keydown.enter="loadVehicle"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;" >
    <el-table :data="vehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"/>
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="上牌时间" align="center"/>
      <el-table-column prop="batteryType" label="电池类型" align="center" :formatter="batteryTypeFormatter"/>
      <el-table-column prop="status" label="车辆状态" align="center" :formatter="vehicleStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary"
                     size="small" @click="editVehicle(scope.row.id)">编辑</el-button>
          <el-button link type="primary"
                     size="small" @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import qs from "qs";
import axios from "axios";

//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增车辆');

// 定义对象用来保存车辆搜索条件,品牌 车牌号
const searchVehicleForm = ref({brand:'',license:''});
// 定义数组，用来保存车辆表格数据：
const vehicleArr = ref([]);
// 定义加载车辆的方法：
const loadVehicle  = ()=>{
  console.log('搜索条件',searchVehicleForm.value);
  let data = qs.stringify(searchVehicleForm.value);
  axios.get(BASE_URL+'/v1/vehicle/select?'+data)
        .then((response)=>{
        if (response.data.code==2000){
         vehicleArr.value=response.data.data;
         console.log("开始打印");
         console.log(response.data);//code、msg、data
         console.log(response.data.data);//是data里的数据
         console.log(vehicleArr.value);
         console.log("查询结果"+vehicleArr.value);
        }else {
          ElMessage.error(response.data.msg);
        }
      })
};


// 定义数组用来保存请求回来的字典数据：
const vehicleTypeArr = ref([]);
const batteryTypeArr = ref([]);

  // onMounted：页面加载完成后执行 VUE的生命周期钩子 在Dom渲染完毕后立即执行
onMounted(()=>{
  loadVehicle();
  loadDictOptions(vehicleTypeArr,'vehicle_type');
  loadDictOptions(batteryTypeArr,'battery_type');
})
// 定义搜索卡片的重置按钮： 清空值+重新加载车辆
const resetSearch = ()=>{
  searchVehicleForm.value = {};
  loadVehicle();
}
// 对车辆状态显示内容进行文字转换
const vehicleStatusFormatter =(row,column,cellValue,index)=>{
  if (cellValue ==1){
    cellValue = '空闲';
  }else if (cellValue == 0){
    cellValue = '占用';
  }
  return cellValue;
}

// 对车辆类型显示进行文字转换
const vehicleTypeFormatter =(row,column,cellValue,index)=>{
  if (!vehicleTypeArr.value){
    return cellValue;
  }
  for (let item of vehicleTypeArr.value){
    if (item.value == cellValue){
      cellValue =item.label;
    }
  }
  // if (cellValue ==10){
  //   cellValue = '轿车';
  // }else if (cellValue == 20){
  //   cellValue = '货车';
  // }else if(cellValue==30){
  //   cellValue='客车';
  // }else if (cellValue==40){
  //   cellValue = '挂车';
  // }
  return cellValue;
}
// 对电池类型显示内容进行文字转换
const batteryTypeFormatter =(row,column,cellValue,index)=>{
  for (let item of batteryTypeArr.value){
    if (item.value==cellValue){
      cellValue =item.label;
    }
  }
  // if (cellValue ==10){
  //   cellValue = '铅酸蓄电池';
  // }else if (cellValue == 20){
  //   cellValue = '镍氢电池';
  // }else if(cellValue==30){
  //   cellValue='钠硫电池';
  // }else if (cellValue==40){
  //   cellValue = '二次锂电池';
  // }else if (cellValue==50){
  //   cellValue = '空气电池';
  // }else if (cellValue == 60){
  //   cellValue = '三元锂电池';
  // }else if (cellValue ==70){
  //   cellValue = '碱性燃料电池';
  // }
  return cellValue;
}
// 定义对象用来收集车辆弹窗表单的所有数据：
const saveVehicleFrom = ref({
  brand:'',
  license:'',
  model:'',
  code:'',
  displacement:'',
  type:'',
  color:'',
  kilometers:'',
  buyTime:'',
  regTime:'',
  price:'',
  batteryType:'',
})

// 定义保存车辆弹窗的标题：
const beforeAddVehicle = ()=>{
  // 确保每次新增时的标题，切弹窗表单中没有数据
  dialogTitle.value="新增车辆";
  dialogVisible.value=true;/*新建车辆弹窗出现*/
  saveVehicleFrom.value={};/*新建车辆清空数据*/
}
//定义保存车辆的方法：
const saveVehicle = ()=>{
  // console.log(saveVehicleFrom.value);
  let data = qs.stringify(saveVehicleFrom.value);
  console.log(data);
  // 给后端发请求：
  axios.post(BASE_URL+'/v1/vehicle/save',data)
      .then((response)=>{
        if (response.data.code==2000){
          ElMessage.success("保存成功");
          dialogVisible.value=false;/*用户保存后关闭弹窗*/
          saveVehicleFrom.value={};/*用户保存后清空数据*/
          loadVehicle();/*重新加载数据*/
        }else {
          // 数据库里的code与 license字段设置了唯一，如果出现了重复值
          // 会操作失败，所以我们可以设置操作失败的提示语句
          // ElMessage.error(response.data.msg);
          ElMessage.error("车牌号与车辆识别码不允许重复");
        }
      });
}


// 1.编辑：定义编辑车辆的方法（只需要写前端就行，复用后端的数据）
const editVehicle = (id)=>{
//  2.让弹窗出来：
  dialogVisible.value=true;
  /*3.修改弹窗标题*/
  dialogTitle.value='编辑车辆';
  /*4.根据id查询当前指定车辆的详细数据  用现成的不能用 /id */
  axios.get(BASE_URL+'/v1/vehicle/select?id='+id)
    .then((response)=>{
      console.log(response.data.data)
      if (response.data.code==2000){
        //5.将数据回显到表单中：将数据赋值给表单响应式变量
        //注意：后端返回的是list<>，所以查一条数据的话拿的是集合的第一个元素data【0】
        saveVehicleFrom.value=response.data.data[0];
      }else {
        ElMessage.error(response.data.msg);
      }
    })
}

// 删除车辆操作：
const deleteVehicle =(id)=>{
  if (confirm("你确认要删除吗")){
    axios.post(BASE_URL+'/v1/vehicle/delete/'+id)
      .then((response)=>{
        if (response.data.code==2000){
          ElMessage.success("恭喜你成功删除");
          loadVehicle();/*删除成功后自动加载页面*/
        }else {
          ElMessage.error("删除失败");
        }
      })
  }
}


// 处理保存车辆弹窗关闭的方法：
const handleClose = ()=>{
  if (confirm("你确认要关闭吗")){
    dialogVisible.value=false;
  }
}
</script>